<?php
	####################################################
	# StrFormat 스트링 포맷                            #
	####################################################

	class StrFormat {
		private static $mobile_phone_number_pattern_to_kor = "/([0-9]{3})([0-9]+)([0-9]{4})/";
		private static $mobile_phone_number_pattern_to_en = "/\+([0-9]{2})(.+?)([0-9]{2})(.+?)([0-9]+)(.+?)([0-9]{4})/"; // +82-10-0200-9988 +82-10-000-9988
		private static $replacement_string = "$1-$2-$3";
		private static $mobile_phone_number_pattern_for_remove_dash = "/-/i";


		# 01089605985 >> 010-8960-5985
		public static function convert_to_mobile_phone_number($arg_p1) {
			$convert_number = $arg_p1;
			$formatted_pnum = preg_replace(StrFormat::$mobile_phone_number_pattern_to_kor, StrFormat::$replacement_string, $convert_number);
			return $formatted_pnum;
		}

		# 010-8960-5985 >> 01089605985
		public static function convert_to_mobile_phone_number_remove_dash($arg_p1) {
			$convert_number = $arg_p1;
			$unformatted_pnum = preg_replace(StrFormat::$mobile_phone_number_pattern_for_remove_dash, "", $convert_number);
			return $unformatted_pnum;
		}

		# naiyumie@gmail.com >> n******e@gmail.com
		public static function marked_email($p1) {
			$str_cn_email = "";
			foreach(str_split(substr($p1,1,strpos($p1,'@')-2)) as $key=>$val){
				$str_cn_email .= '*';
			}
			$splt_email = str_split($p1);
			$ret_email = $splt_email[0].$str_cn_email.substr($p1,strpos($p1,'@')-1,strlen($p1));
			//Nlog::xmp($p1);
			//Nlog::xmp($str_cn_email);
			//Nlog::xmp($ret_email);
			return $ret_email;
		}

		# database text type string output html element
		public static function element($outstream) {
			$outstream = trim($outstream);
			$outstream = stripslashes($outstream);
			$outstream = nl2br($outstream);
			$outstream = htmlspecialchars_decode($outstream);
			return $outstream;
		}

		# 000-000-000 >> 000.000.000
		# Y-m-d >> Y.m.d도 응용.
		public static function dash_to_point($text) {
			$text = str_replace('-','.',$text);
			return $text;
		}

		# abcdefg >> abcd...
		public static function cut_str($text, $length, $suffix="...") {
			if (strlen($text) <= $length) return $text;

			$cpos = $length - 1;
			$count_2B = 0;
			$lastchar = $text[$cpos];

			while (ord($lastchar)>127 && $cpos>=0) {
				$count_2B++;
				$cpos--;
				$lastchar = $text[$cpos];
			}

			return substr($text, 0, (($count_2B % 2) ? $length-1 : $length)).$suffix;
		}

		# 글자르기
		public static function substr_cut($msg, $cut_size, $tail = "...") {
			if ($cut_size <= 0) return $msg;
			$msg = strip_tags($msg);
			$msg = str_replace("&mp;quot;", "\"", $msg);
			if (strlen($msg) <= $cut_size) return $msg;
			for ($i = 0; $i < $cut_size; $i++) if (ord($msg[$i]) > 127) $han++;
			else $eng++;
			if ($han%2) $han--;
			$cut_size = $han + $eng;
			$tmp = substr($msg, 0, $cut_size);
			$tmp .= $tail;
			return $tmp;
		}

		# 문자열을 잘라준다. 한글 가능.
		#  string $str  : 대상 문자열
		# integer $num  : 나타낼 바이트 수
		#  string $tail : 자른뒤에 표시할 문자열
		# ※ 이게 젤좋은듯 싶다.
		public static function substr_kr($str,$len, $tail='..') {
			if (strlen($str) > $num) {
				$c = substr(str_pad(decbin(ord($str{$len})),8,'0',STR_PAD_LEFT),0,2);
				if ($c == '10'){
					for (;$c != '11' && $c{0} == 1;$c = substr(str_pad(decbin(ord($str{--$len})),8,'0',STR_PAD_LEFT),0,2));
				}
				$str =  substr($str,0,$len) . (strlen($str)-strlen($tail) >= $len ? $tail : '');
			}

			return $str;
		}

		# 숫자를 한글로 바꾸기
		public static function num_to_kor($num) {
			$text = '';
			$d_symbol = array('4' => "만", '8' => "억", '12' => "조", '16' => "경", '20' => "해", '24' => "시", '28' => "양", '32' => "구", '36' => "간", '40' => "정", '44' => "재", '48' => "극", '52' => "항하사", '56' => "아승지", '60' => "나유타", '64' => "불가사의", '68' => "무량대수");
			$p_symbol = array('0' => "", '1' => "십", '2' => "백", '3' => "천");
			$t_symbol = array('0' => "", '1' => "일", '2' => "이", '3' => "삼", '4' => "사", '5' => "오", '6' => "육", '7' => "칠", '8' => "팔", '9' => "구");
			if (substr($num, 0, 1) == '-') {
				$num = substr($num, 1);
				$text .= '마이너스';
			}
			$length_of_num = strlen($num);
			if ($length_of_num > 72) {
				$text = "존재할 수 없는 수치 입니다.";
			} else {
				#실행
				for ($k = 0; $k < $length_of_num; $k++) {
					$striped_value = substr($num, $k, 1);
					$text .= $t_symbol[$striped_value];
					$power_value = ($length_of_num - $k - 1) % 4;
					if ($striped_value <> 0) $text .= $p_symbol[$power_value];
					if ($power_value == 0) $text .= $d_symbol[$length_of_num - $k - 1];
				}
			}
			return $text;
		}


		# HTML Tag를 제거
		public static function del_html( $str ) {
			$str = str_replace( ">", "&gt;",$str );
			$str = str_replace( "<", "&lt;",$str );
			return $str;
		}


		# 주민등록번호 포맷
		public static function resident_registration_number($juminno, $cutno = 3, $des = "x", $empty = "&nbsp;") {
			$juminno = str_replace("-", "", $juminno);
			if (strlen($juminno) != 13) return $empty;
			for ($i = 0; $i < $cutno; $i++) $x .= $des;
			$juminno = substr($juminno, 0, 13 - $cutno).$x;
			$juminno = substr($juminno, 0, 6)."-".substr($juminno, 6);
			return $juminno;
		}

		# URL 포맷
		public static function url_href($url, $ltype = false, $title = false, $other = "", $htype = "http://", $empty = "&nbsp;") {
			$url = eregi_replace("http://", "", trim($url));
			if ($url) $url = $htype.$url;
			else return $empty;
			if ($title) $turl = $title;
			else $turl = $url;
			if ($ltype) return "<a href='{$url}' {$other}>{$turl}</a>";
			else return $url;
		}

		# 이메일 포맷
		public static function email($email, $ltype = false, $title = false, $empty = "&nbsp;") {
			$email = trim($email);
			$title = trim($title);
			if (!$email && !$title) return $empty;
			else if (!$email) return $title;
			if ($title) $temail = $title;
			else $temail = $email;
			if ($ltype) return "<a href='mailto:{$email}'>{$temail}</a>";
			else return $email;
		}

		# 전화번호 포맷
		public static function telephone($num1, $num2, $num3, $format = "-", $empty = "&nbsp;") {
			$num1 = trim($num1);
			$num2 = trim($num2);
			$num3 = trim($num3);
			if (!$num1) $num1 = "02";
			if ($num2 && $num3) return $num1.$format.$num2.$format.$num3;
			else return $empty;
		}

		# 문자 포맷
		public static function text($str, $empty = "&nbsp;") {
			$str = trim($str);
			if ($str) return $str;
			else return $empty;
		}

		# rtrim의 랩핑, 여러라인의 공백/탭을 제거한다.
		public static function multy_line_rtrim($str_multiple_line) {
			$ret = NULL;
			$lines = explode(PHP_EOL, $str_multiple_line);
			foreach($lines as $key=>$val){
				$ret .= rtrim(rtrim($val," "),"	").PHP_EOL;
			}
			return $ret;
		}


		# 9와 같거나 작은 숫자를 입력시 0n과 같은 숫자(문자)를 리턴.
		public static function numeric_add_zero($num) {
			if($num <= 9){
				$ret = "0".$num;
			} else {
				$ret = $num;
			}
			return $ret;
		}

		# 단어 필터링
		public static function filter($string,$filter = "",$type = "change",$change = "###") {
			if(!$filter) return $string;
			$filter = explode(",",$filter);
			$fcount = sizeof($filter);
			for($i = 0;$i < $fcount;$i++) {
				if(eregi($filter[$i],$string)) {
					$string = str_replace($filter[$i],$change,$string);
				}
			}
			return $string;
		}

	}

?>